package ru.papergames.battleserver.model.battle.npc;

import ru.common.util.Point3D;
import ru.papergames.battleserver.model.battle.map.Obstacle;

/**
 * @author Alexander Troshanin
 */
public class NpcBattleMap {

    private int width;
    private int height;

    private NpcCell[][] cells;

    public NpcBattleMap(int width, int height) {
        this.width = width;
        this.height = height;
        cells = new NpcCell[width][height];
        for (int x = 0; x < cells.length; x++) {
            for (int y = 0; y < cells[x].length; y++) {
                cells[x][y] = new NpcCell(x, y);
            }
        }
    }

    public void addObstacle(Obstacle obstacle) {
        int leftUpConnerX = obstacle.getLeftUpConnerX();
        int leftUpConnerY = obstacle.getLeftUpConnerY();
        for (Obstacle.BlockedCell blockedCell : obstacle.getBlockedCells()) {
            NpcCell cell = getCell(leftUpConnerX + blockedCell.getX(), leftUpConnerY + blockedCell.getY());
            if (cell != null) {
                cell.setHasObstacle(true);
            }
        }
    }

    public NpcCell getCell(int x, int y) {
        return (x >= 0 && x < width && y >= 0 && y < height) ? cells[x][y] : null;
    }

    public NpcCell getCell(Point3D point) {
        return point != null ? getCell(point.getX(), point.getY()) : null;
    }

    public void addNpcBall(NpcCell cell, NpcBall npcBall) {
        cell.setNpcBall(npcBall);
    }

    public void removeNpcBall(NpcCell cell) {
        cell.removeGameBall();
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public float getCost(int sx, int sy, int tx, int ty) {
        return sx - tx == 0 || sy - ty == 0 ? 10 : 14;
    }

}
